package common;

import common.dal.Transaction;
import common.interfaces.IPersistent;
import common.logger.*;
import common.logger.Logger.LogLevel;

/**
 * An abstract manager class, mostly to facilitate logging actions
 * 
 * @author Gal Lalouche
 */
public abstract class AbstractManager implements IPersistent {
	/**
	 * The logger
	 */
	protected final Logger	logger	= CompositeLogger.getInstance();
	private final String	name;
	
	/**
	 * The transaction this manager works under
	 */
	protected Transaction	t;
	
	/**
	 * C'tor
	 * 
	 * @param t The transaction this manager will work under
	 */
	protected AbstractManager(Transaction t) {
		if (t == null) {
			throw new IllegalArgumentException("t is null");
		}
		
		this.t = t;
		String[] split = getClass().getName().split("\\.");
		name = split[split.length - 1];
	}
	
	private String getManagerName() {
		return name;
	}
	
	/**
	 * Logs that the current method has been called
	 */
	protected void logMethod() {
		String methodName = getCallingMethodsName();
		logger.log(LogLevel.DEBUG, getManagerName() + "." + methodName + " called");
	}
	
	private static String getCallingMethodsName() {
		return Thread.currentThread().getStackTrace()[3].getMethodName();
	}
}
